var my_vue = {};
$(function() {
	$("#left").load("../common/gover-left.html");
	var fromSpace = isEmpty(parseQueryString().fromSpace) ? '0' : parseQueryString().fromSpace;
	var type = isEmpty(parseQueryString().type) ? 'dept' : parseQueryString().type;
	var fromGuide = isEmpty(parseQueryString().fromGuide) ? '0' : parseQueryString().fromGuide;
	var fromWhere = isEmpty(parseQueryString().fromWhere) ? '' : parseQueryString().fromWhere;
	var code = parseQueryString().code;
	var userType = parseQueryString().userType;
	var currInput = $("#myphone");
	$(".input_text").bind('click', function(e) {
		currInput = $(this);
	});

	$(".query_btns .btn").bind('click', function(e) {
		if($(this).attr("value")) {
			switch($(this).attr("value")) {
				case "clear":
					currInput.val("");
					break;
				case "del":
					currInput.val(currInput.val().substring(0, currInput.val().length - 1));
					break;
				default:
					currInput.val(currInput.val() + $(this).attr("value"));
					break;
			}
		}
	});

	var myValidation = $("#validation-btn").btnValidation({
		time: 60,
		txt: "重新获取验证码(n)"
	});

	my_vue = new Vue({
		el: '#vue_info',
		data: {
			Front: device.Front + '?t=' + new Date().getMilliseconds(),
			Back: device.Back + '?t=' + new Date().getMilliseconds(),
			dto: {
				userId: '',
				mobile: '',
				name: '',
				sex: '',
				nation: '',
				birthday: '',
				address: '',
				cardNum: '',
				issued: '',
				life: ''
			},
			welcome: '您好  ',
			oldUser: 0
		},
		methods: {
			toProject: function() {
				validatePhone();
				validateNcode();
				var myphone = $("#myphone").val();
				var ncode = $("#myinput").val();
				if(validatePhone() == false || validateNcode() == false) {
					return false
				} else {
					this.checkIn(myphone, ncode);
				}
			},

			checkIn: function(myphone, ncode) {
				$.ajax({
					type: "post",
					url: gover.signIn,
					async: true,
					dataType: 'json',
					data: {
						mobile: myphone,
						msgCode: ncode,
						userId: this.dto.userId
					},
					success: function(result) {
						doMask();
						if(result.success) {
							my_vue.onlineUser1();
						} else {
							unMask();
							showRedMsg("message", result.message);
						}

					},
					error: function(result) {
						unMask();
						showRedMsg("message", "服务器正忙,请稍候重试");
					}
				});
			},
			toReturn: function() {
				window.location = "readIdentityCard.html";
			},
			validation: function() {
				if(validatePhone() == false) {
					return false
				} else {
					var myphone = $("#myphone").val();
					if(myphone != '' && this.oldUser == 0) {
						this.checkMobile(myphone);
					} else {
						this.getMsgCode(myphone);
					}
				}
			},
			checkMobile: function(mobile) {
				$.ajax({
					type: "get",
					url: gover.checkMobile,
					async: true,
					dataType: 'json',
					data: {
						mobile: mobile
					},
					success: function(result) {
						if(result.success) {
							console.log(result.data.isUsed);
							if(result.data.isUsed == 0) {
								my_vue.getMsgCode(mobile);
							} else {
								showRedMsg('', "该手机号已被注册!");
							}
						} else {
							showRedMsg('', result.message);

						}

					},
					error: function(result) {
						showRedMsg('', "服务器正忙,请稍候重试");
					}
				});
			},
			getMsgCode: function(mobile) {
				var t = new Date().getMilliseconds();
				$.ajax({
					type: "get",
					url: gover.getMsgCode + "?t=" + t,
					async: true,
					dataType: 'json',
					data: {
						userId: this.dto.userId,
						mobile: mobile
					},
					success: function(result) {
						if(result.success) {
							myValidation.start();
							console.log(result.data);
							$("#myinput").val(result.data.msgCode);

						} else {
							showRedMsg('', result.message);
						}

					},
					error: function(result) {
						showRedMsg('', "服务器正忙,请稍候重试");
					}
				});
			},
			setWelcome: function() {
				this.welcome += this.dto.name;
				if(this.dto.sex == '男') {
					this.welcome += "先生";
				} else {
					this.welcome += "女士";
				}

				if(isNotEmpty(this.dto.cardNum)) {
					this.welcome += '(';
					this.welcome += this.dto.cardNum.substring(0, 5);
					this.welcome += '******';
					this.welcome += this.dto.cardNum.substring(13, 18);
					this.welcome += ')';
				}
				if(isNotEmpty(this.dto.mobile)) {
					this.welcome += "  请获取验证码登陆";
				} else {
					this.welcome += "  请输入手机号登入";
				}
			},
			onlineUser1: function() {
				this.$http.post(gover.onlineUser, {
					userId: this.dto.userId,
					mobile: this.dto.mobile,
					name: this.dto.name,
					sex: this.dto.sex,
					nation: this.dto.nation,
					birthday: this.dto.birthday,
					address: this.dto.address,
					cardNum: this.dto.cardNum,
					issued: this.dto.issued,
					life: this.dto.life
				}, {
					emulateJSON: true
				}).then(function(result) {
					unMask();
					if(result.data.success) {
						sessionStorage.setItem("userId", this.dto.userId);
						if(isEmpty(fromWhere)) {
							if(fromSpace == '1') {
								window.location = "../space/home.html";
							} else if(fromGuide == '1') {
								window.location = "chooseServiceType.html?userType=" + userType +
									"&code=" + code;
							} else {
								window.location = "selfHelpBusinessChoose.html?type=" + type;
							}
						} else {
							if(fromWhere=='complain'){
								window.location = "../complain/complain-index.html"
							} else{
								window.location = "../advice/advice-index.html"
							}
						}

					} else {
						unMask();
						showRedMsg("message", "服务器正忙,请稍候重试");
					}
				}, function(result) {
					unMask();
					showRedMsg("message", "服务器正忙,请稍候重试");
				})
			}

		},
		ready: function() { //初始化获得总页面数和列表数据
			this.dto = toObj(sessionStorage.getItem("dto"));
			if(isNotEmpty(this.dto.mobile)) {
				this.oldUser = 1;
				$("#myphone").attr("readOnly", "true");
				$("#myphone").addClass("disabled");
			}
			this.setWelcome();
			var url = "";
			if(this.oldUser == 1) {
				url = "../up/userInfo.html";
			} else {
				url = "../up/newUserInfo.html";
			}
			windowOpen(url);
		}
	});
});